package com.example.yangguoying;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yangguoying.SqLiteDatabase.GetDatabase;
import com.example.yangguoying.datasource.DataSource;
import com.example.yangguoying.entity.StudentInfo;
import com.example.yangguoying.util.JDBC.JDBCAPI.JDBCUtilImpl;
import com.example.yangguoying.util.SolveEditTextScrollClash;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ShowStudentInfoActivity extends AppCompatActivity {

    TextView show_student_name;
    TextView show_student_num;
    TextView show_student_phone;
    TextView show_student_faculty;
    TextView show_student_grade;
    TextView show_student_class;
    TextView show_student_dormitory;
    TextView show_student_address;
    TextView show_student_remarks;
    Button modify_button;
    ImageButton bt_bak;
    GetDatabase database;
    SQLiteDatabase sqLiteDatabase;
    Cursor cursor;
    Intent globalIntent;
    DataSource dataSource;
    JDBCUtilImpl jdbcUtil;
    LinearLayout show_student_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_student_info);
        initView();
        setView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 3:
                StudentInfo studentInfo = queryStudentInfo(globalIntent.getIntExtra("id",-1));
                setShowStudentInfo(studentInfo);
                break;
        }
    }

    private void initView() {
        show_student_name = findViewById(R.id.show_student_name);
        show_student_num = findViewById(R.id.show_student_num);
        show_student_phone = findViewById(R.id.show_student_phone);
        show_student_faculty = findViewById(R.id.show_student_faculty);
        show_student_grade = findViewById(R.id.show_student_grade);
        show_student_class = findViewById(R.id.show_student_class);
        show_student_dormitory = findViewById(R.id.show_student_dormitory);
        show_student_address = findViewById(R.id.show_student_address);
        show_student_remarks = findViewById(R.id.show_student_remarks);
        modify_button = findViewById(R.id.modify_button);
        show_student_layout = findViewById(R.id.show_student_layout);
        dataSource = new DataSource();
        jdbcUtil = new JDBCUtilImpl(dataSource.getConnectionUrl(),dataSource.getDatabaseUser(),dataSource.getDatabasePassword(),"com.mysql.jdbc.Driver");
        bt_bak = findViewById(R.id.bt_back);
        database = new GetDatabase();
        sqLiteDatabase = database.getDatabase(this).getWritableDatabase();
        globalIntent = getIntent();

    }



    private void setView() {
        //TODO 设置ID
        queryStudentInfo(globalIntent.getIntExtra("id",-1));
        modify_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 修改数据
                Intent intent = new Intent(ShowStudentInfoActivity.this,ModifyInfoActivity.class);
                //TODO 设置ID
                intent.putExtra("id",globalIntent.getIntExtra("id",-1));
                startActivityForResult(intent,3);
            }
        });
        bt_bak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private String getEditTextString(EditText editText){
        return editText.getText().toString();
    }

    private StudentInfo queryStudentInfo(int id){
        StudentInfo studentInfo = null;
        if (!globalIntent.getBooleanExtra("isCloud",false)){
            cursor = sqLiteDatabase.query("student_info",null,"student_info_id = ?",new String[]{String.valueOf(id)},null,null,null,null);
            if (cursor.moveToFirst()) {
                do {
                    int studentInfoId = cursor.getInt(cursor.getColumnIndex("student_info_id"));
                    String studentName = cursor.getString(cursor.getColumnIndex("student_name"));
                    String studentNum = cursor.getString(cursor.getColumnIndex("student_num"));
                    String studentPhone = cursor.getString(cursor.getColumnIndex("student_phone"));
                    String studentAddress = cursor.getString(cursor.getColumnIndex("student_address"));
                    String studentFaculty = cursor.getString(cursor.getColumnIndex("student_faculty"));
                    String studentGrade = cursor.getString(cursor.getColumnIndex("student_grade"));
                    String studentClass = cursor.getString(cursor.getColumnIndex("student_class"));
                    String studentDormitory = cursor.getString(cursor.getColumnIndex("student_dormitory"));
                    String studentRemarks = cursor.getString(cursor.getColumnIndex("student_remarks"));
                    int isUpload = cursor.getInt(cursor.getColumnIndex("is_upload"));
                    studentInfo = new StudentInfo(studentInfoId,studentName,studentNum,studentPhone,studentAddress,studentFaculty,studentGrade,studentClass,studentDormitory,studentRemarks,isUpload);
                } while (cursor.moveToNext());
                setShowStudentInfo(studentInfo);
            }
        }else{
            show_student_layout.removeView(modify_button);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ResultSet resultSet = jdbcUtil.queryData("SELECT * FROM student_info where student_info_id = ?", new Object[]{String.valueOf(globalIntent.getIntExtra("id",-1))});
                    StudentInfo studentInfo = null;
                    try {
                        while (resultSet.next()){
                            studentInfo = new StudentInfo(resultSet.getInt("student_info_id"),resultSet.getString("student_name"),resultSet.getString("student_num")
                                    ,resultSet.getString("student_phone"),resultSet.getString("student_address"),resultSet.getString("student_faculty"),resultSet.getString("student_grade")
                                    ,resultSet.getString("student_class"),resultSet.getString("student_dormitory"),resultSet.getString("student_remarks"),resultSet.getInt("is_upload"));
                        }
                    }catch (SQLException e) {
                        e.printStackTrace();
                    }
                    showCloudView(studentInfo);
                }
            }).start();
        }
        return  studentInfo;
    }

    private void setShowStudentInfo(StudentInfo studentInfo){
        show_student_name.setText(studentInfo.getStudentName());
        show_student_num.setText(studentInfo.getStudentNum());
        show_student_phone.setText(studentInfo.getStudentPhone());
        show_student_faculty.setText(studentInfo.getStudentFaculty());
        show_student_grade.setText(studentInfo.getStudentGrade());
        show_student_class.setText(studentInfo.getStudentClass());
        show_student_dormitory.setText(studentInfo.getStudentDormitory());
        show_student_address.setText(studentInfo.getStudentAddress());
        show_student_remarks.setText(studentInfo.getStudentRemarks());
    }

    private void showCloudView(StudentInfo studentInfo) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setShowStudentInfo(studentInfo);

            }
        });
    }
}